package com.example.websocket.websocket.service.serviceImpl;

import com.example.websocket.websocket.Mapper.MessageRecordMapper;
import com.example.websocket.websocket.pojo.Message;
import com.example.websocket.websocket.pojo.MessageRecord;
import com.example.websocket.websocket.service.MessageRecordService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;

import javax.websocket.Session;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @program: websocket
 * @description:
 * @author: fks
 * @create: 2025-03-26 15:45
 **/

@Service
public class MessageRecordServiceImpl implements MessageRecordService {
    @Autowired
    private MessageRecordMapper messageRecordMapper;
    @Override
    public void saveMessage(Message message, String username) {
        MessageRecord messageRecord = new MessageRecord();
        BeanUtils.copyProperties(message,messageRecord);
        messageRecord.setId(String.valueOf(UUID.randomUUID()));
        messageRecord.setFromUser(username);
        messageRecord.setTime(String.valueOf(LocalDateTime.now()));
        messageRecordMapper.insert(messageRecord);
    }

    /**
     * 获取消息
     *
     * @param fromUser,toUser
     * @return
     */
    @Override
    public List<MessageRecord> getMessage( String toUser, String fromUser) {
        return messageRecordMapper.getMessage(toUser,fromUser);
    }

    @Override
    public List<String> getChatFriends(String username) {
        List<String> chatFriend1 = messageRecordMapper.getChatFriend1(username);
        List<String> chatFriend2 = messageRecordMapper.getChatFriend2(username);
        List<String> list = Stream.concat(chatFriend1.stream(), chatFriend2.stream())
                .distinct()
                .collect(Collectors.toList());
        return list;
    }


}
